home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / ParseException.java < prev    next >
Text File  |  1998-06-30  |  7KB  |  211 lines

  1. /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 0.7pre6 */
  2. /*
  3.  * @(#)ParseException.java    1.6 98/03/12
  4.  * 
  5.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  6.  * 
  7.  * This software is the confidential and proprietary information of Sun
  8.  * Microsystems, Inc. ("Confidential Information").  You shall not
  9.  * disclose such Confidential Information and shall use it only in
  10.  * accordance with the terms of the license agreement you entered into
  11.  * with Sun.
  12.  * 
  13.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  14.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  15.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  16.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  17.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  18.  * THIS SOFTWARE OR ITS DERIVATIVES.
  19.  * 
  20.  */
  21. package com.sun.java.swing.text.html;
  22.  
  23. /**
  24.  * This exception is thrown when parse errors are encountered.
  25.  * You can explicitly create objects of this exception type by
  26.  * calling the method generateParseException in the generated
  27.  * parser.
  28.  *
  29.  * You can modify this class to customize your error reporting
  30.  * mechanisms so long as you retain the public fields.
  31.  */
  32. class ParseException extends Exception {
  33.  
  34.   /**
  35.    * This constructor is used by the method "generateParseException"
  36.    * in the generated parser.  Calling this constructor generates
  37.    * a new object of this type with the fields "currentToken",
  38.    * "expectedTokenSequences", and "tokenImage" set.  The boolean
  39.    * flag "specialConstructor" is also set to true to indicate that
  40.    * this constructor was used to create this object.
  41.    * This constructor calls its super class with the empty string
  42.    * to force the "toString" method of parent class "Throwable" to
  43.    * print the error message in the form:
  44.    *     ParseException: <result of getMessage>
  45.    */
  46.   public ParseException(Token currentTokenVal,
  47.                         int[][] expectedTokenSequencesVal,
  48.                         String[] tokenImageVal
  49.                        )
  50.   {
  51.     super("");
  52.     specialConstructor = true;
  53.     currentToken = currentTokenVal;
  54.     expectedTokenSequences = expectedTokenSequencesVal;
  55.     tokenImage = tokenImageVal;
  56.   }
  57.  
  58.   /**
  59.    * The following constructors are for use by you for whatever
  60.    * purpose you can think of.  Constructing the exception in this
  61.    * manner makes the exception behave in the normal way - i.e., as
  62.    * documented in the class "Throwable".  The fields "errorToken",
  63.    * "expectedTokenSequences", and "tokenImage" do not contain
  64.    * relevant information.  The JavaCC generated code does not use
  65.    * these constructors.
  66.    */
  67.  
  68.   public ParseException() {
  69.     super();
  70.     specialConstructor = false;
  71.   }
  72.  
  73.   public ParseException(String message) {
  74.     super(message);
  75.     specialConstructor = false;
  76.   }
  77.  
  78.   /**
  79.    * This variable determines which constructor was used to create
  80.    * this object and thereby affects the semantics of the
  81.    * "getMessage" method (see below).
  82.    */
  83.   protected boolean specialConstructor;
  84.  
  85.   /**
  86.    * This is the last token that has been consumed successfully.  If
  87.    * this object has been created due to a parse error, the token
  88.    * followng this token will (therefore) be the first error token.
  89.    */
  90.   public Token currentToken;
  91.  
  92.   /**
  93.    * Each entry in this array is an array of integers.  Each array
  94.    * of integers represents a sequence of tokens (by their ordinal
  95.    * values) that is expected at this point of the parse.
  96.    */
  97.   public int[][] expectedTokenSequences;
  98.  
  99.   /**
  100.    * This is a reference to the "tokenImage" array of the generated
  101.    * parser within which the parse error occurred.  This array is
  102.    * defined in the generated ...Constants interface.
  103.    */
  104.   public String[] tokenImage;
  105.  
  106.   /**
  107.    * This method has the standard behavior when this object has been
  108.    * created using the standard constructors.  Otherwise, it uses
  109.    * "currentToken" and "expectedTokenSequences" to generate a parse
  110.    * error message and returns it.  If this object has been created
  111.    * due to a parse error, and you do not catch it (it gets thrown
  112.    * from the parser), then this method is called during the printing
  113.    * of the final stack trace, and hence the correct error message
  114.    * gets displayed.
  115.    */
  116.   public String getMessage() {
  117.     if (!specialConstructor) {
  118.       return super.getMessage();
  119.     }
  120.     String expected = "";
  121.     int maxSize = 0;
  122.     for (int i = 0; i < expectedTokenSequences.length; i++) {
  123.       if (maxSize < expectedTokenSequences[i].length) {
  124.         maxSize = expectedTokenSequences[i].length;
  125.       }
  126.       for (int j = 0; j < expectedTokenSequences[i].length; j++) {
  127.         expected += tokenImage[expectedTokenSequences[i][j]] + " ";
  128.       }
  129.       if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
  130.         expected += "...";
  131.       }
  132.       expected += eol + "    ";
  133.     }
  134.     String retval = "Encountered \"";
  135.     Token tok = currentToken.next;
  136.     for (int i = 0; i < maxSize; i++) {
  137.       if (i != 0) retval += " ";
  138.       if (tok.kind == 0) {
  139.         retval += tokenImage[0];
  140.         break;
  141.       }
  142.       retval += add_escapes(tok.image);
  143.       tok = tok.next; 
  144.     }
  145.     retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn + "." + eol;
  146.     if (expectedTokenSequences.length == 1) {
  147.       retval += "Was expecting:" + eol + "    ";
  148.     } else {
  149.       retval += "Was expecting one of:" + eol + "    ";
  150.     }
  151.     retval += expected;
  152.     return retval;
  153.   }
  154.  
  155.   /**
  156.    * The end of line string for this machine.
  157.    */
  158.   protected String eol = System.getProperty("line.separator", "\n");
  159.  
  160.   /**
  161.    * Used to convert raw characters to their escaped version
  162.    * when these raw version cannot be used as part of an ASCII
  163.    * string literal.
  164.    */
  165.   protected String add_escapes(String str) {
  166.       StringBuffer retval = new StringBuffer();
  167.       char ch;
  168.       for (int i = 0; i < str.length(); i++) {
  169.         switch (str.charAt(i))
  170.         {
  171.            case 0 :
  172.               continue;
  173.            case '\b':
  174.               retval.append("\\b");
  175.               continue;
  176.            case '\t':
  177.               retval.append("\\t");
  178.               continue;
  179.            case '\n':
  180.               retval.append("\\n");
  181.               continue;
  182.            case '\f':
  183.               retval.append("\\f");
  184.               continue;
  185.            case '\r':
  186.               retval.append("\\r");
  187.               continue;
  188.            case '\"':
  189.               retval.append("\\\"");
  190.               continue;
  191.            case '\'':
  192.               retval.append("\\\'");
  193.               continue;
  194.            case '\\':
  195.               retval.append("\\\\");
  196.               continue;
  197.            default:
  198.               if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
  199.                  String s = "0000" + Integer.toString(ch, 16);
  200.                  retval.append("\\u" + s.substring(s.length() - 4, s.length()));
  201.               } else {
  202.                  retval.append(ch);
  203.               }
  204.               continue;
  205.         }
  206.       }
  207.       return retval.toString();
  208.    }
  209.  
  210. }
  211.